﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;
using ComicBookApp.DataModels;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace ComicBookApp.Data
{
    public static class Seeder
    {
        public static void Seed(ApplicationDbContext db)
        {
            //seed comics
            db.ComicBooks.AddOrUpdate(
                x => x.Title,
                new ComicBook("Spiderman","A guy gets some spidey sense knocked into him.","http://static.comicvine.com/uploads/original/12/123441/3134929-1.jpg"),
                new ComicBook("Superman","A guy tries to avoid coming into contact with kryptonite.","http://static.comicvine.com/uploads/original/7/72508/1346078-dd8681_superman_comic.jpg"),
                new ComicBook("Batman", "A rich guy dresses up as a bat to fight crime.", "http://upload.wikimedia.org/wikipedia/en/4/4d/BatmanComicIssue1,1940.png")
                );
            db.SaveChanges();
            //seed character
            db.Characters.AddOrUpdate(
                x => x.Name,
                new Character("Peter Parker", "He's a spider and a man.", "http://static.comicvine.com/uploads/original/11113/111130700/3785766-3754492508-36299.jpg", 1),
                new Character("Louis Lane", "She's a journalist in Metropolis.", "http://upload.wikimedia.org/wikipedia/en/9/94/Superman27.PNG", 2),
                new Character("Bruce Wayne", "He's a billionaire.", "http://img2.wikia.nocookie.net/__cb20090604000908/batman/images/4/46/Bruce_Wayne_06.jpg", 3)
                );
                

            //seed creators

            //seed comic-creators

            //seed users
            var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
            roleManager.Create(new IdentityRole { Name = "Admin" });

            db.SaveChanges();

            List<ApplicationUser> users = new List<ApplicationUser>() 
            {
                new ApplicationUser() { UserName="JohnDoe", FirstName="John"},
                new ApplicationUser() { UserName="JaneDoe", FirstName="Jane"},
                new ApplicationUser() { UserName="TimmyDoe", FirstName="Timmy"},
            };



            using(var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db)))
            {
                userManager.Create(users[0], "John123#");
                users[0] = userManager.FindByName(users[0].UserName);
                userManager.Create(users[1], "Jane123#");
                userManager.Create(users[2], "Timmy123#");

                db.SaveChanges();

                userManager.AddToRole(users[0].Id, "Admin");
                
            }
            db.SaveChanges();


            

        }
    }
}
